//
//  ATSetViewController.m
//  Adorable_Things
//
//  Created by lanou on 16/1/19.
//  Copyright © 2016年 Banana Inc. All rights reserved.
//

#import "ATSetViewController.h"
#import "ATSetTableViewCell.h"
#import "ATLoginViewController.h"
#import "ATMsgCenterViewController.h"

@interface ATSetViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation ATSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor orangeColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 65, kWidth, kHeight - 65) style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    _dataArr = [NSMutableArray arrayWithObjects:@"每天提醒我来签到", @"告诉小伙伴", @"清除图片缓存", @"关于萌货", @"退出登录", nil];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        ATSetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ATSetTableViewCell"];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"ATSetTableViewCell" owner:self options:nil] lastObject];
        }
        cell.SignLabel.text = _dataArr[0];
         cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else if (indexPath.row == self.dataArr.count - 1){
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"myCell"];
        }
        ATUserManager *manager = [ATUserManager shareManager];
        if (manager.isLogin == 1) {
            cell.textLabel.text = _dataArr[indexPath.row];
        }else{
            cell.textLabel.text = @"登录";
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;

    }else{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"myCell"];
        }
        cell.textLabel.text = _dataArr[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ATUserManager *manager = [ATUserManager shareManager];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (indexPath.row == 4) {
        if ([cell.textLabel.text isEqualToString:@"退出登录"]) {
            manager.isLogin = 0;
            NSMutableDictionary *newDic = [NSMutableDictionary dictionary];
            newDic[@"isLogin"] = @"0";
            newDic[@"auth_token"] = manager.auth_token;
            [newDic writeToFile:kUserInfor atomically:YES];
            
            cell.textLabel.text = @"登录";
            self.setBlock();
        }else{
          //  manager.isLogin = 1;
            NSMutableDictionary *newDic = [NSMutableDictionary dictionary];
            newDic[@"isLogin"] = @"1";
            newDic[@"auth_token"] = manager.auth_token;
            [newDic writeToFile:kUserInfor atomically:YES];
            
            ATLoginViewController *loginVC = [[ATLoginViewController alloc]init];
            
            loginVC.loginBlock = ^void{
                if (manager.isLogin == 1) {
                    cell.textLabel.text = @"退出登录";
                }else{
                    cell.textLabel.text = @"登录";
                }
                self.setBlock();
            };
            
            [self presentViewController:loginVC animated:YES completion:nil];
            
        }
        
    }else if (indexPath.row == 3) {
        ATMsgCenterViewController *msgVC = [[ATMsgCenterViewController alloc]init];
        self.navigationController.navigationBarHidden = NO;
        [self.navigationController pushViewController:msgVC animated:YES];
      
    }else if (indexPath.row == 1) {
        [UMSocialSnsService presentSnsIconSheetView:self
                                             appKey:@"56971506e0f55abee6001622"
                                          shareText:@"我关注了Adorable_Things"
                                         shareImage:[UIImage imageNamed:@"L108"]
                                    shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToWechatSession,UMShareToWechatTimeline,UMShareToQQ,UMShareToQzone,nil]
                                           delegate:nil];

    }else if(indexPath.row == 2){
        UIAlertView *alterV = [[UIAlertView alloc]initWithTitle:@"提示" message:@"成功清除缓存" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alterV show];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
    [super viewWillAppear:animated];
    ATUserManager *manager = [ATUserManager shareManager];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:5 inSection:0]];
    if (manager.isLogin == 1) {
        cell.textLabel.text = @"退出登录";
    }else{
        cell.textLabel.text = @"登录";
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
